package com.example.origin.demo.demo;

import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RequestMapping("TryCatchDemo")
@RestController
public class TryCatchDemo {
    @GetMapping("test")
    public void test() {
        String s = return1();
        System.out.println(s);
        try {
            s = return2();
        } catch (Exception e) {
        }
        System.out.println(s);

        List list = testTryCatchReturn1();
        System.out.println(list);

        int i = testFinallyReturn();
        System.out.println(i);

    }


    private static String return1() {
        try {
            return "1";
        } catch (Exception e) {
            return "2";
        } finally {
            return "3";
        }
    }

    private static String return2() {
        try {
            return "1";
        } catch (Exception e) {
            return "2";
        } finally {
            int i = 1 / 0;
            return "3";
        }
    }

    public static List testTryCatchReturn1() {
        List res = new ArrayList();
        try {
            res.add(1);
            System.out.println("try ======== res:" + res);
            int a = 1 / 0;
            return res;
        } catch (Exception e) {
            res.add(2);
            System.out.println("catch ======== res:" + res);
            return res;
        } finally {
            res.add(3);
            System.out.println("finally ======== res:" + res);
            res = null;
            System.out.println("finally ======== res:" + res);
        }
    }


    public static int testFinallyReturn() {
        int res = 1;
        try {
            res++;
            System.out.println("try ======== res:" + res);
            int a = 1 / 0;
            return res;
        } catch (Exception e) {
            res++;
            System.out.println("catch ======== res:" + res);
            int a = 1 / 0;
            return res;
        } finally {
            res++;
            System.out.println("finally ======== res:" + res);
            return res;
        }
    }



}
